$(function () {
    let searchBtn = $("#search-place-button");
    let view = map.getView();
    function flyTo(location, done) {
        const duration = 2000;
        const zoom = view.getZoom();
        let parts = 2;
        let called = false;
        function callback(complete) {
            --parts;
            if (called) {
                return;
            }
            if (parts === 0 || !complete) {
                called = true;
                done(complete);
            }
        }
        view.animate(
            {
                center: location,
                duration: duration,
            },
            callback
        );
        view.animate(
            {
                zoom: zoom - 1,
                duration: duration / 2,
            },
            {
                zoom: zoom,
                duration: duration / 2,
            },
            callback
        );
    }
    searchBtn.click(function () {
        $.ajax({
            url: '/map/search/place',
            type: 'get',
            dataType: 'json',
            data: {
                content: $("#search-place-content").val()
            },
            success: function (data) {
                let status = data.status;
                let minLon = data.minLon;
                let maxLon = data.maxLon;
                let minLat = data.minLat;
                let maxLat = data.maxLat;
                let minBound = ol.proj.fromLonLat([minLon, minLat]);
                let maxBound = ol.proj.fromLonLat([maxLon, maxLat]);
                if (status == 'successful') {
                    map.getView().fit(minBound.concat(maxBound));
                }
                else {
                    window.alert("这是个美女，不是地点！");
                }
            }
        })
    })
})